package leetcode.part2;

import org.junit.Test;

/**
 *
 */
public class S209 {
    public int minSubArrayLen(int target, int[] nums) {
        int left = 0;
        int right = 0;
        int len = nums.length;
        if (nums.length <= 1) return nums[0] >= target ? 1 : 0;
        int sum = 0;
        int minLen = nums.length + 1;
        while (right < len) {
            sum += nums[right];
            while (sum >= target) {
                minLen = Math.min(minLen, right - left + 1);
                sum -= nums[left++];
            }
            right++;

        }
        return minLen == nums.length + 1 ? 0 : minLen;

    }

    @Test
    public void test() {
        int[] nums = {2, 3, 1, 2, 4, 3};
        System.out.println(minSubArrayLen(7, nums));
    }
}
